<#

调用示例


# 其他
#>


# 脚本参数定义
param(
    [Parameter(Mandatory=$false)]
    # 使用 [switch] 类型表示布尔开关（无需值）
    [switch]$Test,

    [Parameter(Mandatory=$false)]
    [string]$OutputSavePath = "${env:USERPROFILE}\Downloads"  # 输出保存地址
    # [string]$Format = "png16m",           # 图片格式（png/jpeg）
    # [int]$Resolution = 300             # 分辨率（DPI）
)

$OsVersion = 0;
# 获取操作系统信息
$os = Get-CimInstance -ClassName Win32_OperatingSystem
$version = [version]$os.Version
# Windows 7 对应版本号为 6.1.*（例如 6.1.7601）。
# Windows 10/11 对应版本号为 10.0.*（例如 10.0.19045）。
# 判断系统版本
# 判断系统版本
if ($version.Major -eq 6) {
    switch ($version.Minor) {
        1 { # 6.1.*
            # Write-Output "Windows 7" 
            $OsVersion = 7;
        }       
        2 { # 6.2.*
            # Write-Output "Windows 8" 
            $OsVersion = 8;
        }       
        3 { # 6.3.*
            # Write-Output "Windows 8.1" 
            $OsVersion = 8.1;
        }    
        default { 
            # Write-Output "其他 Windows 版本: $($os.Caption)" 
            $OsVersion = 0;
        }
    }
} elseif ($version.Major -eq 10) {
    # 通过构建号区分 Win10 和 Win11
    if ($version.Build -ge 22000) {          # Win11 构建号 ≥ 22000
        # Write-Output "Windows 11"
        $OsVersion = 11;
    } else {                                 # Win10 构建号 < 22000
        # Write-Output "Windows 10"
        $OsVersion = 10;
    }
} else {
    # Write-Output "未知系统: $($os.Caption)"
    $OsVersion = 0;
}

# 检查是否有某命令
function which ($command) {
  Get-Command -Name $command -ErrorAction SilentlyContinue |
    Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}

# 日志打印
function XWrite-Host ($FLAG,$String) {
    # $NowDateTime = $(Get-Date -f "MM-dd HH:mm:ss")
    $NowDateTime = $(Get-Date -f "HH:mm:ss")
    if ($Test) {# 传递了 --test 参数
        switch($FLAG){
        # switch -Wildcard ( $FLAG ){
            # 'Info' { 
            #     Write-Information "[$FLAG][$NowDateTime] $String ."   
            # }
            { $_ -in "Info", "INFO" } {
                # Write-Information "[$FLAG][$NowDateTime] $String ." 
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor White 
            }
            'Warning' { 
                # Write-Warning -Message "[$FLAG][$NowDateTime] $String ."  
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Yellow  
            }
            { $_ -in "Debug", "DEBUG" } { 
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Blue 
            }
            { $_ -in "Error", "ERROR" } { 
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Red 
                # Write-Error -Message "[$FLAG][$NowDateTime] $String ." 
            }
            default { Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Green }
        }
    } else {
        switch($FLAG){
            { $_ -in "Info", "INFO" }  {
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor White 
            }
            { $_ -in "Error", "ERROR" } { 
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Red 
            }
            default { 
                # Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Green 
            }
        }
    }
}


# 函数 添加系统环境变量
Function Add-EnvVars($AddEnvName,$AddEnvVal,$AddEnvClass) {
                                        # Process 、User 、Machine
    # 获取原来的环境变量
    $OrgPathParts = [System.Environment]::GetEnvironmentVariable("${AddEnvName}", "${AddEnvClass}")
    $PathParts = $OrgPathParts -Split ";"
    # Write-Host "[Debug] $PathParts"; # 打印信息
    if($PathParts -contains $AddEnvVal) { # 已存在了
        # -contains操作符确实很强大，但是很遗憾，它不能在指定字符串中包含通配符。如果想使用通配符进行查找数组元素，则可以使用-like操作符。
        XWrite-Host "Debug" "[$PathParts] exist in [$AddEnvVal]"; # 打印信息
    } else { # 不存在
        # Write-Host "[INFO] $AddEnvName isn't exist $AddEnvVal"; # 打印信息
        $NewPathParts = $PathParts + $AddEnvVal
        # Write-Host "[INFO] PathParts = $PathParts "; # 打印信息
        $NewPath = $NewPathParts -Join ";"
        # Write-Host "[INFO] NewPath == $NewPath  "; # 打印信息
        # [System.Environment]::SetEnvironmentVariable("${AddEnvName}", "${AddEnvVal}",  "${AddEnvClass}")
        [System.Environment]::SetEnvironmentVariable("${AddEnvName}", "${NewPath}",  "${AddEnvClass}")
        # [System.Environment]::SetEnvironmentVariable("${AddEnvName}", "${NewPath}",  "Machine") # 系统环境变量无法使用脚本写入 ，提示不允许所请求的注册表访问权。”
        # 添加完再次查询
        $PathParts = [System.Environment]::GetEnvironmentVariable("${AddEnvName}", "${AddEnvClass}") -Split ";"
        # Write-Host "[INFO] $PathParts"; # 打印信息
        XWrite-Host "INFO" "$PathParts"; # 打印信息
    }
}
# 函数 检查并添加系统环境变量
Function Check-And-Add-USerEnvVars($TargetEnvPath){
    if(Test-Path -Path $TargetEnvPath) {# 文件夹存在
        # Write-Host "[INFO] $TargetEnvPath is exist."; # 打印信息
        if(-Not ($env:PATH.Contains("$TargetEnvPath"))) { # 不存在该环境变量
        Add-EnvVars "PATH" "$TargetEnvPath" "User";
    } else {
        # 
    }
    } else { # 文件夹不存在
        # Write-Host "[INFO] $TargetEnvPath is not exist."; # 打印信息
        XWrite-Host "INFO" "$TargetEnvPath is not exist."; # 打印信息
    }
}

# 下载函数
# 函数 下载
function Invoke-Download
{
    param (
        [Parameter()] [string] $uri = "https://ftp.ncep.noaa.gov/data/nccf/com/hrrr/prod/hrrr.$m2/conus/hrrr.t$m21z.wrfsfcf00.grib2.idx",
        [Parameter()] [string] $outputPath = "C:\OpenGrADS-2.2\data\hrrr\test.grb2",
        [Parameter()] [int] $sleeptimer = 30,
        [Parameter()] [int] $maxTries = 3,
        [Parameter()] [int] $tries = 0
    )
    # 判断是否下载完成存在文件，存在则则不重复下载
    # $TRUE_FALSE=(Test-Path $outputPath)
    # if($TRUE_FALSE -eq "True") {
    if(Test-Path $outputPath) {
        # 文件已经存在
        XWrite-Host "INFO" "$outputPath already exists." 
    } else {
        XWrite-Host "INFO" "This is Invoke-Download(). $uri --> $outputPath" # 打印信息
        try{
            Invoke-WebRequest -Uri $uri -OutFile $outputPath;
        }
        catch [System.Net.WebException] {
            $tries++;
            XWrite-Host "INFO" "The download error is being retried`: $tries" 
            if($tries -lt $maxTries){
                Start-Sleep -Seconds $sleeptimer
                Invoke-Download -uri $uri -outputPath $outputPath -sleeptimer $sleeptimer -maxTries $maxTries -tries $tries
            } else {
                Write-Error ("Tried to download '{0}' {1}-times and failed" -f $uri, $tries);
            }
        }
    }
}

# 函数 
Function Download-Url-File($downloadUrl,$DownSaveDir) {
    XWrite-Host "Debug" "This is Download-Url-File(). "; # 打印信息;
    # 解析 URL 并获取最后一段路径
    $resStr = "^$githubProxyUrl"
    $downloadUrlT = $downloadUrl -replace $resStr, '' # 去掉代理避免后面截取出错
    XWrite-Host "Debug" "downloadUrlT = $downloadUrlT"
    $uri = [System.Uri]$downloadUrlT
    $lastSegment = $uri.Segments[-1]
    # $lastSegment = $downloadUrl.split("/")[-1];
    XWrite-Host "Debug" "lastSegment = $lastSegment"
    # 智能替换文件名逻辑
    if ($lastSegment -match '^(main|master)\.zip$') {
        # $saveFileNameT =  $uri.Segments[5] | ForEach-Object { $_.TrimEnd('/') } # 提取路径段并去除末尾斜杠
        $saveFileNameT =  $uri.Segments[2] | ForEach-Object { $_.TrimEnd('/') } # 取域名后第2个分段路径段并去除末尾斜杠
        $saveFileName =  "$saveFileNameT.zip"
        XWrite-Host "Debug" "saveFileNameT = $saveFileNameT"
        XWrite-Host "Debug" "saveFileName = $saveFileName"
    } else {
        $saveFileName = $lastSegment
    }
    $outputPath = "$DownSaveDir/$saveFileName";
    XWrite-Host "Debug" "The [ $downloadUrl ] file is being downloaded now. "; # 打印信息
    Invoke-Download $downloadUrl $outputPath;
    XWrite-Host "Debug" "The [ $downloadUrl ] file is downloaded and stored in [$outputPath]."; # 打印信息
}

# 静默安装
function installSoftware($softwarePath){
    # # 检查管理员权限
    # if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    #     Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    #     exit
    # }
    # 静默安装
    $logFile = "$softwarePath.log"
    try {
        # Write-Host "正在静默安装 $softwarePath..."
        XWrite-Host "INFO" "Installing silently  $softwarePath..."
        $installArgs = @(
            '/VERYSILENT',
            '/SUPPRESSMSGBOXES',
            '/NORESTART',
            '/SP-',
            "/LOG=$logFile"
        )
        Start-Process -FilePath $softwarePath -ArgumentList $installArgs -Wait -NoNewWindow
        # Write-Host "安装完成！"
        XWrite-Host "INFO" "Installation complete!"
    } catch {
        # Write-Host "安装失败: $_"
        XWrite-Host "INFO" "Installation failed: $_"
        exit 1
    }
    # 验证
}

# 初始化检查函数0: 下载后就是执行文件，移动到目录文件即可
function initMain0($softwareName,$EnvPath,$softwareDownloadUrl){
    XWrite-Host "Debug" "initMain0()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有（ 使用-not运算符判断）
    if ( -not $(which "$softwareName") ) {  # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $EnvPath)) {
            New-Item -Path $EnvPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $EnvPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"
        # 移动
        $cmd_string="Move-Item -Path $DownloadPath -Destination $EnvPath" # 拷贝文件到对应目录下
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令
    } else {
        # 存在
        XWrite-Host "Debug" "$softwareName is exist."
    }

}

# 初始化检查函数0-1: 下载后就是执行文件,但名字不一样，移动到目录文件并重命名
function initMain0-1($softwareName,$EnvPath,$softwareDownloadUrl){
    XWrite-Host "Debug" "initMain0-1()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有（ 使用-not运算符判断）
    if ( -not $(which "$softwareName") ) {  # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $EnvPath)) {
            New-Item -Path $EnvPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $EnvPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"

        # 移动
        $cmd_string="Move-Item -Path $DownloadPath -Destination $EnvPath" # 拷贝文件到对应目录下
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

        # 重命名
        $cmd_string="Rename-Item -Path $EnvPath\$lastSegment -NewName $EnvPath\$softwareName "
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

    } else {
        # 存在
        XWrite-Host "Debug" "$softwareName is exist."
    }

}

# 初始化检查函数1: 下载后解压出来就是执行文件（没有文件夹），直接解压到目的目录即可
function initMain1($softwareName,$ExpandOutPath,$softwareDownloadUrl){
    XWrite-Host "Debug" "initMain1()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有 （使用-not运算符判断）
    if ( -not $(which "$softwareName") ) { # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        # 
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $ExpandOutPath)) {
            New-Item -Path $ExpandOutPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $ExpandOutPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"
        # 解压到指定文件夹或，解压后拷贝到指定位置
        if ($lastSegment -match '(tar)\.gz$') { # 是tar.gz格式
            # Windows 7 默认的 PowerShell 5.1 基于 .NET Framework 4.x，不支持 TarFile 类（需要 .NET 7+ 或 PowerShell 7+）。
            # 若需原生支持，需升级到 PowerShell 7，但 Windows 7 需安装 Extended Security Update。
            # 使用7z解压
            # Install-Module -Name 7Zip4PowerShell -Scope CurrentUser
            $lastSegmentT = $lastSegment  -replace '\.tar\.gz$', '' # 正则替换去除
            if (-not (which "7z")) {
                XWrite-Host "INFO" "The 7-Zip is not installed."
                exit 1
            } else {
                $7zipPath = "7z"
                $tarGzFile = "$DownloadPath"
                XWrite-Host "Debug" "tarGzFile = $tarGzFile"
                $extractDir = "$DownSaveDir\$lastSegmentT"
                XWrite-Host "Debug" "extractDir = $extractDir"
                # 解压 .tar.gz -> 先解压为 .tar，再解包
                & $7zipPath x $tarGzFile -o"$extractDir" -y  # 解压 .tar.gz 到目标目录
                & $7zipPath x "$extractDir\*.tar" -o"$extractDir" -y  # 解压 .tar 文件

                # 移动
                $cmd_string="Move-Item -Path $extractDir\$softwareName`.exe -Destination $ExpandOutPath" # 拷贝文件到对应目录下
                XWrite-Host "INFO" "cmd_string = $cmd_string"
                Invoke-Expression $cmd_string # 执行命令

                # 删除解压过程文件
                if ($lastSegment -match '(tar)\.gz$') { # 是tar.gz格式
                    $cmd_string= "Remove-Item -Recurse -Force -Path $extractDir" 
                    XWrite-Host "INFO" "cmd_string = $cmd_string"
                    Invoke-Expression $cmd_string # 执行命令
                }
            }
        } else {
            $cmd_string="Expand-Archive  -Force -LiteralPath $DownloadPath -DestinationPath $ExpandOutPath" # 在下载保存目录解
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
        }

    } else {
        # 存在
        XWrite-Host "Debug"  "$softwareName is exist."
    }

}
# 初始化检查函数1-1: 下载后解压出来就是执行文件，找到执行文件拷贝到指定目录即可 并重命令
function initMain1-1($softwareName,$ExpandOutPath,$softwareDownloadUrl,$RenameFilepath){
    XWrite-Host "Debug" "initMain1-1()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有
    # 使用-not运算符判断
    if ( -not $(which "$softwareName") ) { # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        # 
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $ExpandOutPath)) {
            New-Item -Path $ExpandOutPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $ExpandOutPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"

        # 解压
        $cmd_string="Expand-Archive -Force -LiteralPath $DownloadPath -DestinationPath $DownSaveDir" # 在下载保存目录解
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令
        # 移动
        $cmd_string="Move-Item -Path $DownSaveDir\$RenameFilepath    -Destination $ExpandOutPath\$softwareName" # 拷贝文件到对应目录下
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        # XWrite-Host "Warning" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

        # 检查是否有环境变量加入，没有则加入

        # 删除解压遗留
        if(Test-Path "$DownSaveDir\$RenameFilepath") {
            # $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath\* "
            # XWrite-Host "INFO" "cmd_string = $cmd_string"
            # Invoke-Expression $cmd_string # 执行命令
            $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$RenameFilepath" 
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
        }

    } else {
        # 存在
        XWrite-Host "Debug" "$softwareName is exist."
    }
}

# 初始化检查函数1-2: 下载后解压出来需要进入一两级目录，找到执行文件拷贝到指定目录即可 并重命令
function initMain1-2($softwareName,$ExpandOutPath,$softwareDownloadUrl,$ExpandTemSubPath,$RenameFilepath){
    XWrite-Host "Debug" "initMain1-2()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有
    # 使用-not运算符判断
    if ( -not $(which "$softwareName") ) { # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        # 
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $ExpandOutPath)) {
            New-Item -Path $ExpandOutPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $ExpandOutPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"

        # 解压
        $cmd_string="Expand-Archive -Force -LiteralPath $DownloadPath -DestinationPath $DownSaveDir" # 在下载保存目录解
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令
        # 移动
        $cmd_string="Move-Item -Path $DownSaveDir\$ExpandTemSubPath\$RenameFilepath    -Destination $ExpandOutPath\$softwareName" # 拷贝文件到对应目录下
        # XWrite-Host "INFO" "cmd_string = $cmd_string"
        XWrite-Host "Warning" "cmd_string = $cmd_string"
        # Invoke-Expression $cmd_string # 执行命令

        # 检查是否有环境变量加入，没有则加入

        # 删除解压遗留
        # if(Test-Path "$DownSaveDir\$ExpandTemSubPath") {
        #     # $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath\* "
        #     # XWrite-Host "INFO" "cmd_string = $cmd_string"
        #     # Invoke-Expression $cmd_string # 执行命令
        #     $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$ExpandTemSubPath" 
        #     XWrite-Host "INFO" "cmd_string = $cmd_string"
        #     Invoke-Expression $cmd_string # 执行命令
        # }

    } else {
        # 存在
        XWrite-Host "Debug" "$softwareName is exist."
    }
}

# 初始化检查函数2: 下载后解压出来需要进入一两级目录，找到执行文件拷贝到指定目录即可
function initMain2($softwareName,$ExpandOutPath,$softwareDownloadUrl,$MoveSubPath){
    XWrite-Host "Debug" "initMain2()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有
    # 使用-not运算符判断
    if ( -not $(which "$softwareName") ) { # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        # 
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $ExpandOutPath)) {
            New-Item -Path $ExpandOutPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $ExpandOutPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"

        # 解压
        $cmd_string="Expand-Archive -Force -LiteralPath $DownloadPath -DestinationPath $DownSaveDir" # 在下载保存目录解
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令
        # 移动
        $cmd_string="Move-Item -Path $DownSaveDir\$MoveSubPath\* -Destination $ExpandOutPath" # 拷贝文件到对应目录下
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

        # 检查是否有环境变量加入，没有则加入

        # 删除解压遗留
        if(Test-Path "$DownSaveDir\$MoveSubPath") {
            # $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath\* "
            # XWrite-Host "INFO" "cmd_string = $cmd_string"
            # Invoke-Expression $cmd_string # 执行命令
            $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath" 
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
        }

    } else {
        # 存在
        XWrite-Host "Debug" "$softwareName is exist."
    }
}
# 初始化检查函数3: 下载后解压出来的文件不是单个可执行文件，拷贝到指定目录，并添加目录到环境变量中
function initMain3($softwareName,$EnvPath,$softwareDownloadUrl,$MoveSubPath){
    XWrite-Host "Debug" "initMain3()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有 （使用-not运算符判断）
    if ( -not $(which "$softwareName") ){ # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        # 
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $EnvPath)) {
            New-Item -Path $EnvPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $EnvPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"
        # 解压
        $cmd_string="Expand-Archive  -Force -LiteralPath $DownloadPath -DestinationPath $DownSaveDir" # 在下载保存目录解
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令
        # 移动
        $cmd_string="Move-Item -Path $DownSaveDir\$MoveSubPath -Destination $EnvPath" # 拷贝文件到对应目录下
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

        # 删除下载文件
        if(Test-Path "$DownSaveDir\$MoveSubPath") {
            # $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath\* "
            # XWrite-Host "INFO" "cmd_string = $cmd_string"
            # Invoke-Expression $cmd_string # 执行命令
            $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath" 
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
        }
        # 添加环境变量
        $fullEnvPath = "$EnvPath\$MoveSubPath"
        XWrite-Host "INFO" "fullEnvPath = $fullEnvPath"
        if (Test-Path -Path $fullEnvPath) { # 存在则j
            Check-And-Add-USerEnvVars "$fullEnvPath"
        } else {
            XWrite-Host "Error"  "$fullEnvPath is not exist."
        }
    } else {
        # 存在
        XWrite-Host "Debug"  "$softwareName is exist."
    }

}

# 初始化检查函数4: 下载后解压出来的文件不是单个可执行文件，拷贝到指定目录，并重命名，并添加目录到环境变量中
function initMain4($softwareName,$EnvPath,$softwareDownloadUrl,$MoveSubPath){
    XWrite-Host "Debug" "initMain4()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有 （使用-not运算符判断）
    if ( -not (Test-Path -Path "$EnvPath\$softwareName" )){ # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $resStr = "^$githubProxyUrl"
        $downloadUrlT = $Url -replace $resStr, '' # 去掉代理避免后面截取出错
        XWrite-Host "Debug" "downloadUrlT = $downloadUrlT"
        $downloadUrl = [System.Uri]$downloadUrlT
        $lastSegment = $downloadUrl.Segments[-1]
        # 智能替换文件名逻辑
        if ($lastSegment -match '^(main|master)\.zip$') {
            $saveFileNameT =  $downloadUrl.Segments[2] | ForEach-Object { $_.TrimEnd('/') } # 取域名后第2个分段路径段并去除末尾斜杠
            $lastSegment =  "$saveFileNameT.zip"
        } 
        XWrite-Host "Debug" "lastSegment = $lastSegment"
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $EnvPath)) {
            New-Item -Path $EnvPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $EnvPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $softwareDownloadUrl $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"
        # 解压
        $cmd_string="Expand-Archive -Force -LiteralPath $DownloadPath -DestinationPath $DownSaveDir" # 在下载保存目录解
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

        # 重命名
        $cmd_string="Rename-Item -Path $DownSaveDir\$MoveSubPath -NewName $DownSaveDir\$softwareName "
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

        # 移动
        $cmd_string="Move-Item -Path $DownSaveDir\$softwareName -Destination $EnvPath" # 拷贝文件到对应目录下
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

        # 删除下载文件
        if(Test-Path "$DownSaveDir\$MoveSubPath") {
            # $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath\* "
            # XWrite-Host "INFO" "cmd_string = $cmd_string"
            # Invoke-Expression $cmd_string # 执行命令
            $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath" 
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
        }
        # 添加环境变量
        $fullEnvPath = "$EnvPath\$softwareName"
        XWrite-Host "INFO" "fullEnvPath = $fullEnvPath"
        if (Test-Path -Path $fullEnvPath) { # 存在则j
            Check-And-Add-USerEnvVars "$fullEnvPath"
        } else {
            XWrite-Host "Error"  "$fullEnvPath is not exist."
        }
    } else {
        # 存在
        XWrite-Host "Debug"  "$softwareName is exist."
    }

}

# 初始化检查函数5: 下载后解压出来的文件不是单个可执行文件，并且没有最外层文件，拷贝到指定目录，并添加目录到环境变量中
function initMain5($softwareName,$EnvPath,$softwareDownloadUrl,$MoveSubPath){
    XWrite-Host "Debug" "initMain5()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有 （使用-not运算符判断）
    if ( -not $(which "$softwareName") ){ # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        # 
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $EnvPath)) {
            New-Item -Path $EnvPath -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $EnvPath" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"
        # 解压
        $cmd_string="Expand-Archive  -Force -LiteralPath $DownloadPath -DestinationPath $DownSaveDir\$MoveSubPath" # 在下载保存目录解
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令
        # 移动
        $cmd_string="Move-Item -Path $DownSaveDir\$MoveSubPath -Destination $EnvPath" # 拷贝文件到对应目录下
        XWrite-Host "INFO" "cmd_string = $cmd_string"
        Invoke-Expression $cmd_string # 执行命令

        # 删除下载文件
        if(Test-Path "$DownSaveDir\$MoveSubPath") {
            # $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath\* "
            # XWrite-Host "INFO" "cmd_string = $cmd_string"
            # Invoke-Expression $cmd_string # 执行命令
            $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath" 
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
        }
        # 添加环境变量
        $fullEnvPath = "$EnvPath\$MoveSubPath"
        XWrite-Host "INFO" "fullEnvPath = $fullEnvPath"
        if (Test-Path -Path $fullEnvPath) { # 存在则
            Check-And-Add-USerEnvVars "$fullEnvPath"
        } else {
            XWrite-Host "Error"  "$fullEnvPath is not exist."
        }
    } else {
        # 存在
        XWrite-Host "Debug"  "$softwareName is exist."
    }

}

# 初始化检查函数5-1: 下载后解压出来的文件是多级单个可执行文件，并且没有最外层文件，拷贝到指定目录，并添加目录到环境变量中
function initMain5-1($softwareName,$EnvDir,$softwareDownloadUrl,$MoveSubPath,$EnvPath){
    XWrite-Host "Debug" "initMain5()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有 （使用-not运算符判断）
    if ( -not $(which "$softwareName") ){ # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        # 
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        if (-not (Test-Path -Path $EnvDir)) {
            New-Item -Path $EnvDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $EnvDir" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"
        if(Test-Path "$EnvPath") {
            # 已存在
        } else {
            # 解压
            $cmd_string="Expand-Archive -Force -LiteralPath $DownloadPath -DestinationPath $DownSaveDir" # 在下载保存目录解
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
            # 移动
            $cmd_string="Move-Item -Path $DownSaveDir\$MoveSubPath -Destination $EnvDir" # 拷贝文件到对应目录下
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
        }
        # 删除下载文件
        if(Test-Path "$DownSaveDir\$MoveSubPath") {
            # $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath\* "
            # XWrite-Host "INFO" "cmd_string = $cmd_string"
            # Invoke-Expression $cmd_string # 执行命令
            $cmd_string= "Remove-Item -Recurse -Force -Path $DownSaveDir\$MoveSubPath" 
            XWrite-Host "INFO" "cmd_string = $cmd_string"
            Invoke-Expression $cmd_string # 执行命令
        }
        # 添加环境变量
        $fullEnvPath = "$EnvPath"
        XWrite-Host "INFO" "fullEnvPath = $fullEnvPath"
        if (Test-Path -Path $fullEnvPath) { # 存在则j
            Check-And-Add-USerEnvVars "$fullEnvPath"
        } else {
            XWrite-Host "Error"  "$fullEnvPath is not exist."
        }
    } else {
        # 存在
        XWrite-Host "Debug"  "$softwareName is exist."
    }

}

# 初始化检查函数6: 下载后需要执行安装的软件
function initMain6($softwareName,$softwareDownloadUrl){
    XWrite-Host "Debug" "initMain6()"
    XWrite-Host "Debug" "softwareName = $softwareName"
    XWrite-Host "Debug" "softwareDownloadUrl = $softwareDownloadUrl"
    # 检查是否有 （使用-not运算符判断）
    if ( -not $(which "$softwareName") ) { # 不存在，则下载解压，添加环境变量
        $Url = $softwareDownloadUrl
        # 解析 URL 并获取最后一段路径
        $downloadUrl = [System.Uri]$Url
        $lastSegment = $downloadUrl.Segments[-1]
        # 
        $DownSaveDir = "${env:USERPROFILE}\Downloads\WinSoftwares" # 下载存放位置变量
        # 创建目标目录（如果不存在）
        if (-not (Test-Path -Path $DownSaveDir)) {
            New-Item -Path $DownSaveDir -ItemType Directory -Force | Out-Null
            XWrite-Host "INFO" "A directory has been created: $DownSaveDir" -ForegroundColor Cyan # 已创建目录
        }
        # 下载
        Download-Url-File $Url $DownSaveDir
        $DownloadPath = "$DownSaveDir\$lastSegment" # 下载存放位置变量
        XWrite-Host "Debug" "DownloadPath = $DownloadPath"
        # 执行静默安装
        installSoftware $DownloadPath   
        # 删除下载文件
    } else {
        # 存在
        XWrite-Host "Debug"  "$softwareName is exist."
    }
}

# 检查并创建写入配置文件
function Check-And-CreateWrite-Config($config_file_path,$config_txt){
    # $config_file_path = "C:\Tools\bin\BBDown.config"
    #echo "[debug] file_path = $file_path"
    # 判断是否下载完成存在文件，存在则进行解压
    $TRUE_FALSE=(Test-Path $config_file_path)
    if($TRUE_FALSE -eq "True") { 
        # 文件存在
        # echo "[debug] BBDown_config_file_path It is exits."
    } else {
        # 文件不存在
        #echo "[debug] BBDown_config_file_path It is not exits."
        # echo "--work-dir $HOME/Downloads `n --skip-ai False " > "C:\Tools\bin\BBDown.config"
        echo "$config_txt" > "$config_file_path"
    }
}

# 函数入口
function main(){
    XWrite-Host "Debug" "Main()"
    XWrite-Host "Debug" "This is InitEnv.ps1"
    # Write-Host "-1--- PSScriptRoot = $PSScriptRoot --------"
    # 初始化依赖
    $githubProxyUrl = "https://github.moeyy.xyz/"
    # $githubProxyUrl = ""
    # 方式零： 下载文件就是可执行的命令文件，下载后直接放到指定目录
    # - 对比工具
    # initMain0 "windows_czkawka_cli" "C:\Tools\bin" "${githubProxyUrl}https://github.com/qarmin/czkawka/releases/download/9.0.0/windows_czkawka_cli.exe"
    initMain0-1 "czkawka_cli.exe" "C:\Tools\bin" "${githubProxyUrl}https://github.com/qarmin/czkawka/releases/download/9.0.0/windows_czkawka_cli.exe"
    # - zip unzip压缩解压
    initMain0 "zip" "C:\Tools\bin" "http://stahlworks.com/dev/zip.exe"
    initMain0 "unzip" "C:\Tools\bin" "http://stahlworks.com/dev/unzip.exe"
    # - 阅读器
    # initMain0 "Readest_0.9.23_x64-portable" "C:\Tools\bin" "${githubProxyUrl}https://github.com/readest/readest/releases/download/v0.9.23/Readest_0.9.23_x64-portable.exe"
    initMain0-1 "Readest.exe" "C:\Tools\bin" "${githubProxyUrl}https://github.com/readest/readest/releases/download/v0.9.23/Readest_0.9.23_x64-portable.exe"
    # - 下载器
    initMain0 "yt-dlp" "C:\Tools\bin" "${githubProxyUrl}https://github.com/yt-dlp/yt-dlp/releases/download/2025.02.19/yt-dlp.exe"
    # initMain0 "glint-windows" "C:\Tools\bin" "${githubProxyUrl}https://github.com/brigand/glint/releases/download/v6.3.4/glint-windows"
    initMain0-1 "glint" "C:\Tools\bin" "${githubProxyUrl}https://github.com/brigand/glint/releases/download/v6.3.4/glint-windows"
    # initMain0 "Wave-win32-x64-0.11.2.exe" "C:\Tools\bin" "${githubProxyUrl}https://github.com/wavetermdev/waveterm/releases/download/v0.11.2/Wave-win32-x64-0.11.2.exe"
    initMain0-1 "Wave.exe" "C:\Tools\bin" "${githubProxyUrl}https://github.com/wavetermdev/waveterm/releases/download/v0.11.2/Wave-win32-x64-0.11.2.exe"
    # - 7-zip
    initMain0 "7zr" "C:\Tools\bin" "https://www.7-zip.org/a/7zr.exe"


    # 方式一： 下载文件是压缩包，解压出来就是执行文件（没有文件夹），直接解压到目的目录即可
    # - m3u8视频下载
    # - [nilaoda/N_m3u8DL-RE · GitHub](https://github.com/nilaoda/N_m3u8DL-RE/releases)
    initMain1 "N_m3u8DL-RE" "C:\Tools\bin" "${githubProxyUrl}https://github.moeyy.xyz/https://github.com/nilaoda/N_m3u8DL-RE/releases/download/v0.3.0-beta/N_m3u8DL-RE_v0.3.0-beta_win-x64_20241203.zip"
    # - git命令行工具 lazygit
    if ($OsVersion -eq 7) {
        # win7 
        initMain1 "lazygit" "C:\Tools\bin" "${githubProxyUrl}https://github.com/jesseduffield/lazygit/releases/download/v0.49.0/lazygit_0.49.0_Windows_x86_64.zip"  
    } else {
        initMain1 "lazygit" "C:\Tools\bin" "${githubProxyUrl}https://github.com/jesseduffield/lazygit/releases/download/v0.42.0/lazygit_0.42.0_Windows_x86_64.zip"  
    }
    # - git命令行工具 lazygit
    initMain1 "gitui" "C:\Tools\bin" "${githubProxyUrl}https://github.com/gitui-org/gitui/releases/download/v0.27.0/gitui-win.tar.gz"
    # - 模糊查找工具
    initMain1 "fzf" "C:\Tools\bin" "${githubProxyUrl}https://github.com/junegunn/fzf/releases/download/0.46.1/fzf-0.46.1-windows_amd64.zip" 

    # - B站视频下载命令行工具
    initMain1 "BBDown" "C:\Tools\bin" "${githubProxyUrl}https://github.com/nilaoda/BBDown/releases/download/1.6.3/BBDown_1.6.3_20240814_win-x64.zip" 
    Check-And-CreateWrite-Config "C:\Tools\bin\BBDown.config" "--work-dir $HOME/Downloads `n --skip-ai False " # BBDown 配置
    # - 下载器
    initMain1 "lux" "C:\Tools\bin" "${githubProxyUrl}https://github.moeyy.xyz/https://github.com/iawia002/lux/releases/download/v0.24.1/lux_0.24.1_Windows_x86_64.zip" 
    # 
    initMain1 "termscp" "C:\Tools\bin" "${githubProxyUrl}https://github.com/veeso/termscp/releases/download/v0.16.1/termscp-v0.16.1-x86_64-pc-windows-msvc.zip" 
    # lf
    # [GitHub - gokcehan/lf: Terminal file manager](https://github.com/gokcehan/lf)
    initMain1 "lf" "C:\Tools\bin" "${githubProxyUrl}https://github.com/gokcehan/lf/releases/download/r34/lf-windows-amd64.zip" 


    # 方式二： 
    if ($OsVersion -eq 7) {
        # 注 如果是win7系统，ffmpeg不要用最新版本，可用4.4版本
        # [ffmpeg Old Versions Downloads - VideoHelp](https://www.videohelp.com/software/ffmpeg/old-versions)
        # https://www.videohelp.com/download/ffmpeg-4.4-full_build.7z
        initMain2 "ffmpeg" "C:\Tools\bin" "https://www.videohelp.com/download-wpnhGmBnMZr/ffmpeg-4.4-full_build.7z" "ffmpeg-4.4-full_build\bin"
    } else {
        initMain2 "ffmpeg" "C:\Tools\bin" "${githubProxyUrl}https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-lgpl.zip"  "ffmpeg-master-latest-win64-lgpl\bin"
    }

    # 方式三：
    # - 命令行输出美化 bat
    initMain3 "bat" "C:\Tools" "${githubProxyUrl}https://github.com/sharkdp/bat/releases/download/v0.25.0/bat-v0.25.0-x86_64-pc-windows-gnu.zip"  "bat-v0.25.0-x86_64-pc-windows-gnu"
    # - 终端文件管理器 yazi
    initMain3 "yazi" "C:\Tools" "${githubProxyUrl}https://github.com/sxyazi/yazi/releases/download/v25.4.8/yazi-x86_64-pc-windows-msvc.zip"  "yazi-x86_64-pc-windows-msvc"
    # - git-cc
    initMain5 "git-cc" "C:\Tools" "${githubProxyUrl}https://github.com/SKalt/git-cc/releases/download/v0.2.5/git-cc_0.2.5_windows_amd64.zip"  "git-cc_0.2.5_windows_amd64"

    # 方式四：
    # - 快速跳转 z.lua
    initMain4 "z.lua" "C:\Tools" "${githubProxyUrl}https://github.com/skywind3000/z.lua/archive/refs/tags/1.8.20.zip" "z.lua-1.8.20"
    # - axel
    initMain4 "axel"  "C:\Tools" "${githubProxyUrl}https://github.com/nishad/axel-for-windows/archive/refs/heads/master.zip"  "axel-for-windows-master"
    
    # 方式五：
    # - 快速跳转 zoxide
    initMain5 "zoxide" "C:\Tools" "${githubProxyUrl}https://github.com/ajeetdsouza/zoxide/releases/download/v0.9.7/zoxide-0.9.7-x86_64-pc-windows-msvc.zip"  "zoxide-0.9.7-x86_64-pc-windows-msvc"
    # zoxide import --from=z "${HOME}\.zlua" # z.lua将数据导入zoxide

    # - rg
    # [GitHub - BurntSushi/ripgrep: ripgrep recursively searches directories for a regex pattern while respecting your gitignore](https://github.com/BurntSushi/ripgrep)
    initMain5-1 "rg" "C:\Tools" "${githubProxyUrl}https://github.com/BurntSushi/ripgrep/releases/download/14.1.1/ripgrep-14.1.1-x86_64-pc-windows-msvc.zip" "ripgrep-14.1.1-x86_64-pc-windows-msvc"  "C:\Tools\ripgrep-14.1.1-x86_64-pc-windows-msvc"

    initMain5-1 "fd" "C:\Tools" "${githubProxyUrl}https://github.com/sharkdp/fd/releases/download/v10.1.0/fd-v10.1.0-x86_64-pc-windows-msvc.zip" "fd-v10.1.0-x86_64-pc-windows-msvc"  "C:\Tools\fd-v10.1.0-x86_64-pc-windows-msvc"
    # - 图片终端预览 imgcat
    # initMain5 "imgcat" "C:\Tools" "${githubProxyUrl}https://github.com/danielgatis/imgcat/releases/download/v1.0.21/imgcat_1.0.21_windows_amd64.zip" "imgcat_1.0.21_windows_amd64" 
    # -  图片终端预览 chafa
    initMain5-1 "chafa" "C:\Tools" "https://hpjansson.org/chafa/releases/static/chafa-1.14.5-1-x86_64-windows.zip" "chafa-1.14.5-1-x86_64-win"  "C:\Tools\chafa-1.14.5-1-x86_64-win"
    # - vim
    # [GitHub - vim/vim-win32-installer: Vim Win32 Installer](https://github.com/vim/vim-win32-installer/)
    # initMain6 "gvim" "https://github.com/vim/vim-win32-installer/releases/download/v9.1.0/gvim_9.1.0_x64_signed.exe"
    initMain5-1 "vim" "C:\Tools" "${githubProxyUrl}https://github.com/vim/vim-win32-installer/releases/download/v9.1.0/gvim_9.1.0_x64_signed.zip" "vim" "C:\Tools\vim\vim91"


    # - neovim
    initMain5-1 "nvim" "C:\Tools" "${githubProxyUrl}https://github.com/neovim/neovim/releases/download/v0.11.0/nvim-win64.zip" "nvim-win64" "C:\Tools\nvim-win64\bin"

    # - helix
    initMain5-1 "hx" "C:\Tools" "${githubProxyUrl}https://github.com/helix-editor/helix/releases/download/25.01.1/helix-25.01.1-x86_64-windows.zip" "helix-25.01.1-x86_64-windows"  "C:\Tools\helix-25.01.1-x86_64-windows"
    
    # - pdftoppm
    initMain5-1 "pdftoppm" "C:\Tools" "${githubProxyUrl}https://github.com/oschwartz10612/poppler-windows/releases/download/v24.08.0-0/Release-24.08.0-0.zip" "poppler-24.08.0"  "C:\Tools\poppler-24.08.0\Library\bin"


    # 方式六：下载需要执行安装的软件
    # - git
    initMain6 "git" "${githubProxyUrl}https://github.com/git-for-windows/git/releases/download/v2.49.0.windows.1/Git-2.49.0-64-bit.exe"

    # - jq
    # [Download jq](https://jqlang.org/download/)
    initMain0-1 "jq.exe" "C:\Tools\bin" "${githubProxyUrl}https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-windows-amd64.exe"


    # - resvg
    # [GitHub - linebender/resvg: An SVG rendering library.](https://github.com/linebender/resvg)
    # https://github.com/linebender/resvg/releases/download/v0.44.0/resvg-explorer-extension.exe
    # https://github.com/linebender/resvg/releases/download/v0.44.0/viewsvg-win64.zip
    # https://github.com/linebender/resvg/releases/download/v0.44.0/resvg-win64.zip
    # https://github.com/linebender/resvg/releases/download/v0.44.0/usvg-win64.zip
    initMain1 "resvg.exe" "C:\Tools\bin" "${githubProxyUrl}https://github.com/linebender/resvg/releases/download/v0.44.0/resvg-win64.zip"
    initMain5 "viewsvg.exe" "C:\Tools" "${githubProxyUrl}https://github.com/linebender/resvg/releases/download/v0.44.0/viewsvg-win64.zip" "viewsvg-win64" 
    initMain1 "usvg.exe" "C:\Tools\bin" "${githubProxyUrl}https://github.com/linebender/resvg/releases/download/v0.44.0/usvg-win64.zip" 
    initMain0 "resvg-explorer-extension.exe" "C:\Tools\bin"  "${githubProxyUrl}https://github.com/linebender/resvg/releases/download/v0.44.0/resvg-explorer-extension.exe"


    # - 直观的查找和替换CLI（sed替代方案） sd
    # [GitHub - chmln/sd: Intuitive find & replace CLI (sed alternative)](https://github.com/chmln/sd)
    initMain5-1 "sd" "C:\Tools" "${githubProxyUrl}https://github.com/chmln/sd/releases/download/v1.0.0/sd-v1.0.0-x86_64-pc-windows-msvc.zip" "sd-v1.0.0-x86_64-pc-windows-msvc"  "C:\Tools\sd-v1.0.0-x86_64-pc-windows-msvc"


    # - 美观实用的终端界面 gum
    # https://github.com/charmbracelet/gum/releases/download/v0.16.0/gum_0.16.0_Windows_x86_64.zip
    initMain5-1 "gum" "C:\Tools" "${githubProxyUrl}https://github.com/charmbracelet/gum/releases/download/v0.16.0/gum_0.16.0_Windows_x86_64.zip" "gum_0.16.0_Windows_x86_64"  "C:\Tools\gum_0.16.0_Windows_x86_64"

    # 用于 git、diff 和 grep 输出的语法高亮分页器 delta
    initMain5-1 "delta" "C:\Tools" "${githubProxyUrl}https://github.com/dandavison/delta/releases/download/0.18.2/delta-0.18.2-x86_64-pc-windows-msvc.zip" "delta-0.18.2-x86_64-pc-windows-msvc"  "C:\Tools\delta-0.18.2-x86_64-pc-windows-msvc"

    # 命令行录制工具 用于将终端的操作过程录制成视频 vhs
    # https://github.com/charmbracelet/vhs/
    initMain5-1 "vhs" "C:\Tools" "${githubProxyUrl}https://github.com/charmbracelet/vhs/releases/download/v0.9.0/vhs_0.9.0_Windows_x86_64.zip" "vhs_0.9.0_Windows_x86_64"  "C:\Tools\vhs_0.9.0_Windows_x86_64"

    # - 用Go编写控制台界面的快速磁盘使用率分析器 gdu
    # https://github.com/dundee/gdu/
    initMain1-1 "gdu.exe" "C:\Tools\bin" "${githubProxyUrl}https://github.com/dundee/gdu/releases/download/v5.30.1/gdu_windows_amd64.exe.zip"  "gdu_windows_amd64.exe"

    # - dust
    # [GitHub - bootandy/dust: A more intuitive version of du in rust](https://github.com/bootandy/dust)
    initMain5-1 "dust" "C:\Tools" "${githubProxyUrl}https://github.com/bootandy/dust/releases/download/v1.2.0/dust-v1.2.0-x86_64-pc-windows-msvc.zip" "dust-v1.2.0-x86_64-pc-windows-msvc"  "C:\Tools\dust-v1.2.0-x86_64-pc-windows-msvc"
    # - dua
    initMain5-1 "dua" "C:\Tools" "${githubProxyUrl}https://github.com/Byron/dua-cli/releases/download/v2.30.0/dua-v2.30.0-x86_64-pc-windows-msvc.zip" "dua-v2.30.0-x86_64-pc-windows-msvc"  "C:\Tools\dua-v2.30.0-x86_64-pc-windows-msvc"
   
}

# 开始执行
# $startTime = Get-Date # 记录开始时间(简单易用，无需额外对象	精度较低（最低到毫秒）)
# # 这里是你的脚本代码
# Start-Sleep -Seconds 5  # 示例：模拟耗时操作
# # 记录结束时间并计算总耗时
# $endTime = Get-Date
# $duration = $endTime - $startTime
# # 输出格式化时间（例如：00:00:05.123）
# Write-Host "脚本执行时间: $($duration.ToString('hh\:mm\:ss\.fff'))"

$stopwatch = [System.Diagnostics.Stopwatch]::StartNew() # 创建并启动计时器 (高精度)
XWrite-Host "Debug" "stopwatch = $stopwatch"
# 这里是你的脚本代码
# Start-Sleep -Seconds 5  # 示例：模拟耗时操作
if ($Test) { # 检查是否传递了 -Test 参数
    # Write-Host "测试模式"
    Write-Host "Test Mode"
    main 
} else {
    # Write-Host "普通模式"
    main
}

# 停止计时器并获取结果
$stopwatch.Stop()
$elapsedTime = $stopwatch.Elapsed

# Write-Host "脚本执行时间: $($elapsedTime.ToString('hh\:mm\:ss\.fff'))" # 输出格式化时间（例如：00:00:05.123）
XWrite-Host "Debug" "Script execution time`: $($elapsedTime.ToString('hh\:mm\:ss\.fff')) "# 输出格式化时间（例如：00:00:05.123）
